package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.testapplication.bean.UserInfo;
import com.example.testapplication.database.UserDBHelper;
import com.example.testapplication.util.DataUtil;

public class SQLiteWriteActivity extends AppCompatActivity implements View.OnClickListener {
    private UserDBHelper mHelper; // 声明一个用户数据库帮助器的对象
    private EditText qt_name;
    private EditText qt_age;
    private EditText qt_height;
    private EditText qt_weight;
    private boolean bMarried = false;
    private String[] typeArray = {"未婚", "已婚"};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_write);
        qt_name = findViewById(R.id.qt_name);
        qt_age = findViewById(R.id.qt_age);
        qt_height = findViewById(R.id.qt_height);
        qt_weight = findViewById(R.id.qt_weight);
        findViewById(R.id.qbtn_save).setOnClickListener(this);
        initTypeSpinner();
    }

    private void initTypeSpinner() {
        ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,R.layout.item_select,typeArray);
        typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
        Spinner sp_married = findViewById(R.id.qp_married);
        sp_married.setPrompt("请选择婚姻状况");
        sp_married.setAdapter(typeAdapter);
        sp_married.setSelection(0);
        sp_married.setOnItemSelectedListener(new TypeSelectedListener());
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.qbtn_save){
            String name = qt_name.getText().toString();
            String age = qt_age.getText().toString();
            String height = qt_height.getText().toString();
            String weight = qt_weight.getText().toString();

            if (TextUtils.isEmpty(name)) {
                showToast("请先填写姓名");
                return;
            } else if (TextUtils.isEmpty(age)) {
                showToast("请先填写年龄");
                return;
            } else if (TextUtils.isEmpty(height)) {
                showToast("请先填写身高");
                return;
            } else if (TextUtils.isEmpty(weight)) {
                showToast("请先填写体重");
                return;
            }
            // 声明用户信息对象
            UserInfo info = new UserInfo();
            info.name = name;
            info.age = Integer.parseInt(age);
            info.height = Long.parseLong(height);
            info.weight = Float.parseFloat(weight);
            info.married = bMarried;
            info.update_time = DataUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss");
            // 执行数据帮助器的插入操作
            mHelper.insert(info);
            showToast("数据库插入数据成功");

            Intent intent = new Intent(this,SQLiteReadActivity.class);
            startActivity(intent);
        }

    }

    private class TypeSelectedListener implements android.widget.AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            bMarried = (i==0)?false:true;
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }

    // 生命周期

    @Override
    protected void onStart() {
        super.onStart();
        // 获取数据库帮助器的实例
        mHelper = UserDBHelper.getInstance(this,2);
        // 打开数据库帮助器的写连接
        mHelper.openWriteLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 关闭数据库连接
        mHelper.closeLink();
    }
    private void showToast(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

}